<template>
  <div id="app">
    <div id="nav">
      <router-link to="/home">SpringbootHome</router-link>&nbsp;
      <router-link to="/userinfo">UserInfo</router-link>&nbsp;
      <a href="/ready">ready</a>&nbsp;
      <a href="/manager">manager</a>&nbsp;
      
      <router-link to="/logout2" v-if="showLogout">logout</router-link>&nbsp;
      <a @click="testU">testU</a>&nbsp;
      <a @click="setCD">测试encrypt</a>
      <p v-if="showLogout">用户名：{{nickname}}</p>
      <input type="text" v-model="tst" />
    </div>
    <router-view />
  </div>
</template>
<script>
import { Message } from "element-ui";

import utils from "./utils/utils";
export default {
  name: "app",
  data() {
    return {
      name: "mm",
      pass: "我的官tytytyty",
      okToken: null,
      nickname: "请登陆",
      enpass: null,
      pubKey: null,
      tst:'/api/',
      showLogout:false
    };
  },
  mounted() {
    this.nickname = utils.getCookie("nickname");
    //var ob={'nickname':this.nickname,'token':localStorage.getItem('token')}
    //window.parent.postMessage(ob,'http://www.bbb.com');
    //event.source.postMessage
    //this.setCD();
    if(utils.getCookie('nickname')){
      this.showLogout=true;
    }
  },
  components: {},

  methods: {
    getPubKey: function() {
      return this.$axios
        .get("/api/getPasswprdKey")
        .then(res => {
          if (res.data.code == "200") {
            //console.log("公钥为：" + res.data.result);
            this.pubKey = res.data.result;
            return Promise.resolve();
          } else if (res.data.code == "500") {
            Message.error("服务器异常，请稍后再试");
          }
        })
        .catch(err => {
          Message.error("getPubKey出现异常，请重试");
        });
    },
    setReady: function() {
      if (utils.getCookie("nickname")) {
        this.nickname = utils.getCookie("nickname");
      }
    },
    setCD: function() {
      this.getPubKey()
        .then(data => {
          if (this.pubKey) {
            console.log("ccc公钥为：" + this.pubKey);
            var en = new JSEncrypt();
            en.setPublicKey(this.pubKey);
            this.enpass = en.encrypt(this.pass);
            console.log("ccc密文为：" + this.enpass);
            return Promise.resolve(this.enpass);
          }
          return Promise.resolve();
        })
        .then(data2 => {
          if (data2 != null && data2 != "") {
            console.log("data2为：" + data2);
            var params = new URLSearchParams();
            params.append("enpass", data2);
            this.$axios
              .post("/api/postDecryptCode", params)
              .then(res => {
                
                
              })
              .catch(err => {
                
                Message.error("setCD出现异常，请重试");
              });
          }
        });
    },
    testPost: function() {
      var params = new URLSearchParams();
      params.append("enpass", "post_7878787");
      //params.append("password", _this.password);
      this.$axios.post("/api/postDecryptCode", params);
    },
    testU:function() {
      
      this.$axios.get(this.tst).then(res=>{
        if(res.data.code=='200'){
          Message.success(res.data.message);
        }
      });
    },
  }
};
</script>
<style lang="scss">
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
#nav {
  padding: 30px;
  a {
    font-weight: bold;
    color: #2c3e50;
    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style>
